package com.unionftech.common.base.mvp.impl

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.unionftech.common.base.BaseFragment
import com.unionftech.common.base.mvp.IBaseView
import com.unionftech.common.base.mvp.proxy.ProxyMvpFragment

abstract class BaseMvpFragment : BaseFragment(), IBaseView {
    private lateinit var mProxyMvpFragment: ProxyMvpFragment<in IBaseView>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mProxyMvpFragment = createProxyFragment()
        mProxyMvpFragment.bindPresenter()
    }

    private fun createProxyFragment(): ProxyMvpFragment<in IBaseView> {
        if (!::mProxyMvpFragment.isInitialized) {
            mProxyMvpFragment = ProxyMvpFragment(this)
        }
        return mProxyMvpFragment
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun showLoading() {
        if (needShowLoading()) {
            super.showLoading()
        }
    }

    override fun hideLoading() {
        if (needShowLoading()) {
            super.hideLoading()
        }
    }

    open fun needShowLoading(): Boolean {
        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        mProxyMvpFragment.unbindPresenter()
        if (needShowLoading()) {
            hideLoading()
        }
    }
}